package com.eztalks.braintree;

import java.math.BigDecimal;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Customer;
import com.braintreegateway.CustomerRequest;
import com.braintreegateway.Environment;
import com.braintreegateway.PaymentMethod;
import com.braintreegateway.PaymentMethodRequest;
import com.braintreegateway.Result;
import com.braintreegateway.Transaction;
import com.braintreegateway.TransactionRequest;
import com.braintreegateway.ValidationError;

/**
 * java-api： https://developers.braintreepayments.com/start/hello-server/java
 * 
 * https://developers.braintreepayments.com/reference/request/customer/create/
 * java
 * 
 * 测试卡：https://developers.braintreepayments.com/guides/credit-cards/testing-go-
 * live/java#valid-card-numbers
 * 
 * @author liurong
 *
 * @date 2018年9月21日
 */
public class BraintreeExample {
	static final BraintreeGateway gateway = new BraintreeGateway(Environment.SANDBOX, "ryzvj274ysrcb5pq",
			"93856p7xkh4rxcp9", "3a20e8f4d982469268d33aebf23b5fab");

	/**
	 * 单次购买
	 */
	@Test
	public void onePayment() {

		TransactionRequest request = new TransactionRequest().amount(new BigDecimal("1000.00"))
				.paymentMethodNonce("3bb3f42f-0c31-0083-7cd4-6991f1d8135f").options().submitForSettlement(true).done();

		Result<Transaction> result = gateway.transaction().sale(request);

		if (result.isSuccess()) {
			Transaction transaction = result.getTarget();
			System.out.println("Success!: " + transaction.getId());
		} else if (result.getTransaction() != null) {
			Transaction transaction = result.getTransaction();
			System.out.println("Error processing transaction:");
			System.out.println("  Status: " + transaction.getStatus());
			System.out.println("  Code: " + transaction.getProcessorResponseCode());
			System.out.println("  Text: " + transaction.getProcessorResponseText());
		} else {
			for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
				System.out.println("Attribute: " + error.getAttribute());
				System.out.println("  Code: " + error.getCode());
				System.out.println("  Message: " + error.getMessage());
			}
		}
	}

	/**
	 * 绑定卡片
	 */
	@Test
	public void createPaymentMethod() {
		PaymentMethodRequest request = new PaymentMethodRequest().customerId("233092453")
				.paymentMethodNonce("b7d3447b-be67-04a5-6d82-d90f7781ba67");

		Result<? extends PaymentMethod> result = gateway.paymentMethod().create(request);

		if (result.isSuccess()) {
			PaymentMethod aMethod = (PaymentMethod) result;
			System.out.println(aMethod.getToken());
		} else {
			System.out.println(JSON.toJSONString(result, true));
		}

	}

	/**
	 * 循环扣费
	 */
	@Test
	public void cycleBilling() {
		try {
			TransactionRequest transactionRequest = new TransactionRequest().amount(new BigDecimal("13.00"))
					.paymentMethodToken("cw8s67").recurring(true).options().submitForSettlement(true).done();

			Result<Transaction> result = gateway.transaction().sale(transactionRequest);

			System.out.println(JSON.toJSONString(result, true));

			// if (result.isSuccess()) {
			// Transaction transaction = result.getTarget();
			// System.out.println("Success!: " + transaction.getId());
			// } else if (result.getTransaction() != null) {
			// Transaction transaction = result.getTransaction();
			// System.out.println("Error processing transaction:");
			// System.out.println(" Status: " + transaction.getStatus());
			// System.out.println(" Code: " +
			// transaction.getProcessorResponseCode());
			// System.out.println(" Text: " +
			// transaction.getProcessorResponseText());
			// } else {
			// for (ValidationError error :
			// result.getErrors().getAllDeepValidationErrors()) {
			// System.out.println("Attribute: " + error.getAttribute());
			// System.out.println(" Code: " + error.getCode());
			// System.out.println(" Message: " + error.getMessage());
			// }
			// }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void createCustomer() {
		CustomerRequest request = new CustomerRequest().firstName("Mark").lastName("Jones").company("Jones Co.")
				.email("yanmy@hst.com").fax("419-555-1234").phone("614-555-1234").website("http://example.com")
				.paymentMethodNonce("0176a550-2955-083e-73ef-3c1eba0a017c");
		Result<Customer> result = gateway.customer().create(request);

		System.out.println(JSON.toJSONString(result, true));

		// result.isSuccess();
		// true

		// result.getTarget().getId();
	}

	@Test
	public void setting() {
		Result<Transaction> result = gateway.transaction().submitForSettlement("95dytwvj");
		System.out.println(JSON.toJSONString(result, true));
		if (result.isSuccess()) {
			Transaction settledTransaction = result.getTarget();
		} else {
			System.out.println(result.getErrors());
		}
	}

	@Test
	public void getCustomer() {
		Customer find = gateway.customer().find("233092453");
		System.out.println(JSON.toJSONString(find, true));
	}
}
